package cn.wy.pet.service.impl;

import cn.wy.pet.domain.PetType;
import cn.wy.pet.mapper.PetTypeMapper;
import cn.wy.pet.service.PetTypeService;
import cn.wy.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements PetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;
    @Override
    public List<PetType> petTypeTree() {
        List<PetType> petTypes = petTypeMapper.findAll();
        Map<Long,PetType> map = new HashMap<>();
        for (PetType petType : petTypes) {
            map.put(petType.getId(),petType);
        }
        List<PetType> petTypeTree = new ArrayList<>();

        for (PetType petType : petTypes) {
            if(petType.getParentId()==null){
                petTypeTree.add(petType);
            }else {
                PetType parentPetType = map.get(petType.getParentId());
                if (parentPetType != null) {
                    parentPetType.getChildren().add(petType);
                }
            }
        }
        return petTypeTree;
    }
}
